package com.hgd.evp.common.entity;

import com.hgd.evp.common.constances.CrtAndUpdFieldConstances;
import com.hgd.evp.common.context.CurrentContext;
import com.hgd.evp.common.utils.BeanUtil;
import com.hgd.evp.common.utils.EmptyUtil;
import com.hgd.evp.common.utils.StringUtil;

import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;

public class EntityUtil {

    public static <T> void initCrtAndUpdateInfo(T entity) throws InvocationTargetException, IllegalAccessException {
        initCrtInfo(entity);
        initUpdInfo(entity);
    }

    public static <T> void initCrtInfo(T entity) throws InvocationTargetException, IllegalAccessException {
        String userId = CurrentContext.getUserId();
        String userName = CurrentContext.getUserName();
        String tenantId = CurrentContext.getTenantId();

        PropertyDescriptor[] pds = BeanUtil.getPropertyDescriptors(entity.getClass());

        if (EmptyUtil.isEmpty(pds)) {
            return;
        }

        for (PropertyDescriptor pd : pds) {
            if (StringUtil.equals(pd.getName(), CrtAndUpdFieldConstances.CRT_USER_ID)) {
                // 找到crtUserId属性
                _setProperty(entity, userId, pd);
            }
            if (StringUtil.equals(pd.getName(), CrtAndUpdFieldConstances.CRT_USER_NAME)) {
                // 找到crtUserName
                _setProperty(entity, userName, pd);
            }
            if (StringUtil.equals(pd.getName(), CrtAndUpdFieldConstances.TENANT_ID)) {
                // 找到tenantId
                _setProperty(entity, tenantId, pd);
            }
            if (StringUtil.equals(pd.getName(), CrtAndUpdFieldConstances.CRT_TIME)) {
                // 找到crtTime
                Method writeMethod = pd.getWriteMethod();
                writeMethod.invoke(entity, new Date());
            }
        }
    }

    private static <T> void _setProperty(T entity, String propertyValue, PropertyDescriptor pd)
            throws IllegalAccessException, InvocationTargetException {
        Class<?> propertyType = pd.getPropertyType();
        String typeName = propertyType.getName();
        Method writeMethod = pd.getWriteMethod();
        Method readMethod = pd.getReadMethod();
        if (StringUtil.equals(typeName, "java.lang.Integer")) {
            Object readMethodValue = readMethod.invoke(entity);
            if (EmptyUtil.isNotEmpty(propertyValue) && EmptyUtil.isEmpty(readMethodValue)) {
                writeMethod.invoke(entity, Integer.valueOf(propertyValue));
            }
        } else if (StringUtil.equals(typeName, "java.lang.String")) {
            Object readMethodValue = readMethod.invoke(entity);
            if (EmptyUtil.isNotEmpty(propertyValue) && EmptyUtil.isEmpty(readMethodValue)) {
                writeMethod.invoke(entity, propertyValue);
            }
        }
    }

    public static <T> void initUpdInfo(T entity) throws InvocationTargetException, IllegalAccessException {
        String userId = CurrentContext.getUserId();
        String userName = CurrentContext.getUserName();

        PropertyDescriptor[] pds = BeanUtil.getPropertyDescriptors(entity.getClass());

        if (EmptyUtil.isEmpty(pds)) {
            return;
        }

        for (PropertyDescriptor pd : pds) {
            if (StringUtil.equals(pd.getName(), CrtAndUpdFieldConstances.UPD_USER_ID)) {
                // 找到crtUserId属性
                _setProperty(entity, userId, pd);
            }
            if (StringUtil.equals(pd.getName(), CrtAndUpdFieldConstances.UPD_USER_NAME)) {
                // 找到crtUserName
                _setProperty(entity, userName, pd);
            }
            if (StringUtil.equals(pd.getName(), CrtAndUpdFieldConstances.UPD_TIME)) {
                // 找到updTime
                Method writeMethod = pd.getWriteMethod();
                writeMethod.invoke(entity, new Date());
            }
        }
    }
}
